#include "my_alloc.h"
#include "lib_syscall.h"

static header_t *head = (header_t *)0;

void *malloc(size_t size)
{
    size            = align(size + sizeof(header_t));
    header_t *block = head;
    while (block)
    {
        if (block->is_free && block->size >= size)
        {
            block->is_free = 0;
            return (void *)(block + 1);
        }
        block = block->next;
    }
    block = sbrk(size);
    if (block == (void *)-1)
    {
        return (void *)0;
    }
    block->size    = size;
    block->is_free = 0;
    block->next    = head;
    head           = block;
    return (void *)(block + 1);
}
void free(void *ptr)
{
    if (!ptr)
        return;
    header_t *block = (header_t *)ptr - 1;
    block->is_free  = 1;
}